using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace MicroMsg.UI.Util
{
	public class UIElementTraveler
	{
		public static UIElement FindElementRecursive(FrameworkElement parent, Type targetType)
		{
			int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
			UIElement result = null;
			if (childrenCount > 0)
			{
				for (int i = 0; i < childrenCount; i++)
				{
					object child = VisualTreeHelper.GetChild(parent, i);
					if (child.GetType() == targetType)
					{
						return child as UIElement;
					}
					result = UIElementTraveler.FindElementRecursive(VisualTreeHelper.GetChild(parent, i) as FrameworkElement, targetType);
				}
			}
			return result;
		}
		public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
		{
			int childrenCount = VisualTreeHelper.GetChildrenCount(parentElement);
			if (childrenCount == 0)
			{
				return default(T);
			}
			for (int i = 0; i < childrenCount; i++)
			{
				DependencyObject child = VisualTreeHelper.GetChild(parentElement, i);
				if (child != null && child is T)
				{
					return (T)((object)child);
				}
				T t = UIElementTraveler.FindFirstElementInVisualTree<T>(child);
				if (t != null)
				{
					return t;
				}
			}
			return default(T);
		}
		public static VisualStateGroup FindVisualState(FrameworkElement element, string name)
		{
			if (element == null)
			{
				return null;
			}
			IList visualStateGroups = VisualStateManager.GetVisualStateGroups(element);
			IEnumerator enumerator = visualStateGroups.GetEnumerator();
			VisualStateGroup result;
			try
			{
				while (enumerator.MoveNext())
				{
					VisualStateGroup visualStateGroup = (VisualStateGroup)enumerator.get_Current();
					if (visualStateGroup.get_Name() == name)
					{
						result = visualStateGroup;
						return result;
					}
				}
				goto IL_53;
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			return result;
			IL_53:
			return null;
		}
		public static List<UIElement> FindAllElement(FrameworkElement parent, Type targetType)
		{
			List<UIElement> list = new List<UIElement>();
			int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
			if (childrenCount > 0)
			{
				for (int i = 0; i < childrenCount; i++)
				{
					object child = VisualTreeHelper.GetChild(parent, i);
					if (child.GetType() == targetType)
					{
						list.Add((UIElement)child);
					}
					else
					{
						List<UIElement> list2 = UIElementTraveler.FindAllElement(VisualTreeHelper.GetChild(parent, i) as FrameworkElement, targetType);
						if (list2 != null)
						{
							for (int j = 0; j < list2.get_Count(); j++)
							{
								list.Add(list2.get_Item(j));
							}
						}
					}
				}
			}
			return list;
		}
	}
}
